import { Controller, Post, Get, Body, UseGuards, Request } from '@nestjs/common';
import { UserService } from './user.service'
import { RegisteByEmailDto } from './dto/registerByEmail.dto'
import { sendcodeByEmail } from './dto/sendcodeByEmail.dto'
import { LoginOrRegisterSuccess } from './classes/user'
import { AuthGuard } from '@nestjs/passport';
import { LoginByPasswordDto } from './dto/loginByPassword.dto'
import { LoginByCodeDto } from './dto/loginByCode.dto'
import { userInfo } from './classes/user'
import { ForgetPasswordSendCodeDto } from './dto/forgetPasswordSendCode.dto'
import {
  ApiResponse,
  ApiTags,
  ApiQuery,
  ApiBearerAuth,
  ApiBody,
  ApiParam,
  ApiHeader
} from '@nestjs/swagger';

@ApiBearerAuth()
@ApiTags('用户')
@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) { }
  @ApiBody({ type: LoginByPasswordDto })
  @ApiResponse({
    status: 200,
    description: '发送成功',
    type: LoginOrRegisterSuccess,
  })
  @Post('loginByPassword')
  async loginByPassword(@Body() loginByPasswordDto: LoginByPasswordDto) {
    return this.userService.loginByPassword(loginByPasswordDto);
  }

  @ApiBody({ type: LoginByCodeDto })
  @ApiResponse({
    status: 200,
    description: '发送成功',
    type: LoginOrRegisterSuccess,
  })
  @Post('loginByCode')
  async loginByCode(@Body() loginByCodeDto: LoginByCodeDto) {
    return this.userService.loginByCode(loginByCodeDto)
  }

  @ApiBody({ type: sendcodeByEmail })
  @ApiResponse({
    status: 200,
    description: '发送成功',
  })
  @Post("loginSendCode")
  async loginSendCode(@Body() Email: sendcodeByEmail) {
    return this.userService.loginSendCode(Email)
  }

  @ApiBody({ type: RegisteByEmailDto })
  @ApiResponse({
    status: 200,
    description: '发送成功',
    type: LoginOrRegisterSuccess,
  })
  @Post("registerByEmail")
  async registerByEmail(@Body() registerDto: RegisteByEmailDto) {
    return this.userService.registerByEmail(registerDto);
  }

  @ApiBody({ type: sendcodeByEmail })
  @ApiResponse({
    status: 200,
    description: '发送成功',
  })
  @Post("registerSendcodeByEmail")
  async register_sendcodeByEmail(@Body() Email: sendcodeByEmail) {
    return await this.userService.registerSendcodeByEmail(Email);
  }

  @ApiHeader({
    name: 'access_token',
    required: true,
    description: '请求请带上token',
  })
  @ApiResponse({
    status: 200,
    description: '发送成功',
    type: userInfo
  })
  @UseGuards(AuthGuard('jwt'))
  @Post("getUserInfo")
  async getUserInfo(@Request() req) {
    return this.userService.getUserInfo(req.user);
  }

  @Post("forgetPasswordSendCode")
  async forgetPasswordSendCode(forgetPasswordSendCodeDto: ForgetPasswordSendCodeDto) {
    return this.userService.forgetPasswordSendCode(forgetPasswordSendCodeDto);
  }
}
